package com.dong.cloud.service;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author DJ
 * @description
 * @date 2022/1/15 3:21 下午
 **/
@Service
public class PaymentService {

    /**
     * 正常的方法
     *
     * @param id
     * @return java.lang.String
     * @author DJ
     * @date 3:22 下午 2022/1/15
     **/
    public String paymentInfoOK(Integer id) {
        return "线程池：" + Thread.currentThread().getName() + "paymentInfoOK,id" + id + "O(∩_∩)O哈哈~";
    }

    /**
     * 模拟超时方法 利用hystrix 如果响应超时就进行降级，给出友好提示
     *
     * @param id
     * @return java.lang.String
     * @author DJ
     * @date 3:22 下午 2022/1/15
     **/
    @HystrixCommand(fallbackMethod = "paymentInfoTimeoutHandler", commandProperties = {
            // 设置属性参数 N s未返回就执行降级操作
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
    })
    public String paymentInfoTimeout(Integer id) {
        int timeNumber = 3000;
        // 异常
        // int age = 10 / 0;
        try {
            TimeUnit.MILLISECONDS.sleep(timeNumber);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池：" + Thread.currentThread().getName() + "paymentInfoTimeout,id" + id + "耗时(秒)" + timeNumber;
    }

    // ---------------------------------------hystrix服务熔断-------------------------------------------------

    /**
     * 熔断接口 @HystrixCommand 添加熔断参数
     *
     * @param id id
     * @return java.lang.String
     * @author DJ
     * @date 9:50 下午 2022/1/16
     **/
    @HystrixCommand(fallbackMethod = "paymentCircuitBreakerFallback", commandProperties = {
            // 是否开启断路器 默认是true
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            // 请求次数 ，默认是20次
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            // 时间窗口（时间段）默认是 5000 单位ms
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            // 失败率阈值 达到多少触发熔断 默认是 50%
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")
    })
    public String paymentCircuitBreaker(Integer id) {
        if (id < 0) {
            throw new RuntimeException(">>>>>id不能为负数");
        }
        String serialNumber = IdUtil.simpleUUID();
        return Thread.currentThread().getName() + "\t" + "服务调用成功, 流水号为:" + serialNumber;
    }

    /**
     * 服务熔断之前的降级 方法，给出服务熔断提示
     *
     * @param id
     * @return java.lang.String
     * @author DJ
     * @date 9:40 下午 2022/1/16
     **/
    public String paymentCircuitBreakerFallback(Integer id) {
        return "id不能为负数，请稍后重试! o(╥﹏╥)o/~~ id:" + id;
    }

    /**
     * fallbackMethod
     *
     * @param id
     * @return java.lang.String
     * @author DJ
     * @date 1:38 下午 2022/1/16
     **/
    public String paymentInfoTimeoutHandler(Integer id) {
        return "线程池：" + Thread.currentThread().getName() + "8001服务系统繁忙或出现异常，请稍后重试, id" + id + "o(╥﹏╥)o";
    }
}
